﻿namespace LogManager.Gui.UI.Representations
{
    using LogManager.Core.Models;
    using LogManager.Core.Services;
    using LogManager.Gui.UI.Helpers;
    using System.Windows.Documents;
    using System.Windows.Media;

    public class DetailsCreator
    {
        public static FlowDocument DetailsFor(object item)
        {
            if (item is ILocation)
            {
                return DetailsFor((ILocation)item);
            }
            else if (item is IServer)
            {
                return DetailsFor((IServer)item);
            }
            else if (item is IPlateform)
            {
                return DetailsFor((IPlateform)item);
            }
            else if (item is IWarehouse)
            {
                return DetailsFor((IWarehouse)item);
            }
            else if (item is SimpleServiceResult)
            {
                return DetailsFor((SimpleServiceResult)item);
            }
            else if (item is LogFile)
            {
                return DetailsFor((LogFile)item);
            }
            else
            {
                var doc = new FlowDocument();
                doc.addTextPair("object: ", item != null ? item.ToString() : "null");
                return doc;
            }
        }

        public static FlowDocument DetailsFor(ILocation location)
        {
            var doc = new FlowDocument();
            doc.addTextTitle("Répertoire");
            doc.addTextPair("Type logs: ", location.LogType.ToString());
            doc.addTextPair("Chemin source: ", location.FullPathSource);
            doc.addTextPair("Chemin destination: ", location.FullPathDestination);
            return doc;
        }

        public static FlowDocument DetailsFor(IServer server)
        {
            var doc = new FlowDocument();
            doc.addTextTitle("Serveur");
            doc.addTextPair("Nom: ", server.Name);
            doc.addTextPair("Jours de rétention: ", server.RetentionHours.ToString());
            return doc;
        }

        public static FlowDocument DetailsFor(IPlateform plateform)
        {
            var doc = new FlowDocument();
            doc.addTextTitle("Plateforme");
            doc.addTextPair("Nom: ", plateform.Name);
            doc.addTextPair("Destination: ", plateform.Depository);
            return doc;
        }

        public static FlowDocument DetailsFor(IWarehouse warehouse)
        {
            var doc = new FlowDocument();
            doc.addTextTitle("Dépôt");
            doc.addTextPair("Nom: ", warehouse.Name);
            doc.addTextPair("Ip: ", warehouse.Ip);
            doc.addTextPair("Domain: ", warehouse.Domain);
            return doc;
        }

        public static FlowDocument DetailsFor(SimpleServiceResult accessTestResult)
        {
            var doc = new FlowDocument();

            if (accessTestResult.Succeeded)
            {
                doc.Foreground = Brushes.Green;
                doc.addTextPair("Réussi: ", accessTestResult.Message);
            }
            else
            {
                doc.Foreground = Brushes.Red;
                doc.addTextPair("Echoué: ", accessTestResult.Message);
            }

            return doc;
        }

        public static FlowDocument DetailsFor(LogFile log)
        {
            var doc = new FlowDocument();

            if (log.Error != null)
            {
                doc.addTextTitle("En erreur");
                doc.addTextPair("Erreur: ", log.Error.Message);
            }

            if (log.ArchivedFile == null)
            {
                doc.addTextTitle("Fichier");
                doc.addTextPair("Nom: ", log.SourceFile.FullName);
                doc.addTextPair("Nom si archivé: ", log.ProjectionPath);
                doc.addTextPair("Date de modification: ", log.SourceFile.LastWriteTime.ToString());
                doc.addTextPair("Taille : ", string.Format(" {0:n2} kB", log.SourceFile.Length / 1000.0));
            }
            else
            {
                doc.addTextTitle("Archivé:");
                doc.addTextPair("Nom archivé: ", log.ArchivedFile.FullName);
                doc.addTextPair("Taille archivé: ", string.Format(" {0:n2} kB", log.ArchivedFile.Length / 1000.0));
            }

            return doc;
        }
    }
}
